<?php
namespace application\views;
use application\models\Content;
use application\models\User;
use application\utilities\Utility;
use application\system\Gandalf;

class Form extends \application\auth\Normal {
	
	protected $content;
	
	public function load() {
		global $concerto;
		$concerto->view = $this;
		require_once CN_APPLICATION_PATH.'application/functions/front-end.php';
	
		switch(getQueryVar('form')) {
			case 'add_content':
			case 'edit_content':
				// Get data fields for that content type
				if($id = getQueryVar('id')) {
					$content = new Content('id='.$id);
					if($_REQUEST['form_submit']) {
						Gandalf::authenticateSender();
						$content->save();
					}
					$content->initForm();
					
					
				}
				elseif($type = getQueryVar('type')) {
					$content = new Content('type='.$type);
					if($_REQUEST['form_submit']) {
						Gandalf::authenticateSender();
						$content->save();
					}
					
					$content->initForm();
				}
				else {
					Utility::triggerError('Content form cannot be generated without type or id.');
				}
				
				
				if($content) {
					$this->content = $content;
					/**
					 * Is there a file corresponding to that content type in the "forms" folder
					 * in the template directory? If so, load that file. If not, load our
					 * default form.
					 */
					$typeName = DB()->getVar("SELECT `ct_permalink` FROM `cn_content_types` WHERE `ct_id`='".$content->getData('content_type')."'");
					$fileName = $typeName . '.php';
					$altFileName = 'content.php';
					
				}
				break;
			case 'login':
			case 'signup':
				if(loggedIn()) {
					$user = new \application\models\User;
				}
				else {
					$user = $concerto->User;
				}
				
				$user->initForm(getQueryVar('form'));
				
				if($_REQUEST['form_submit']) {
					Gandalf::authenticateSender();
					// User has a bunch of different form types so we don't call save() here, we let the
					// validateForm method do it
					$user->validateForm();
				}
				
				
				$fileName = getQueryVar('form') . '.php';
				$altFileName = $fileName;				
				break;
			case 'edit_profile':
				
				// User editing his own profile
				if(getQueryVar('id') == CurrentUser()->getDBField('user_id') || !getQueryVar('id')) {
					$user = $concerto->User;
					
				}
				// Admin editing someone else's profile
				else {
					$user = new \application\models\User(getQueryVar('id'));
				}
				$user->initForm('edit_profile');
				if($_REQUEST['form_submit']) {
					Gandalf::authenticateSender();
					
					$user->validateForm();
				}
				$fileName = getQueryVar('form') . '.php';
				$altFileName = $fileName;
				break;
			case 'add_user':
				$user = new \application\models\User;
				$user->initForm();
				if($_REQUEST['form_submit']) {
					Gandalf::authenticateSender();
					$user->validateForm();
				}
				$fileName = 'add_user.php';
				$altFileName = $fileName;
				break;
			case 'forgot_password':
				$user = new \application\models\User;
				$user->initForm('forgot_password');
				if($_REQUEST['form_submit']) {
					Gandalf::authenticateSender();
					$user->validateForm();
				}
				$fileName = 'forgot_password.php';
				$altFileName = $fileName;
				break;
				
			default:
				// 404 error
				break;
		}
		if($user) {
			$this->content = $user;
		}
		
		Gandalf::generateToken();
		if($fileName) {
			if(file_exists(templateDir() . 'forms/' . $fileName)) {
				include templateDir() . 'forms/' . $fileName;
			}
			else if(file_exists(CN_APPLICATION_PATH . 'application/htmlincludes/forms/' .$altFileName)) {
				include CN_APPLICATION_PATH . 'application/htmlincludes/forms/' .$altFileName;
			}
			else {
				Utility::triggerError('No form file found for this request.');
			}
		}
		else {
			Utility::triggerError('No form file generated for this request.');
		}
	}
	
	public function formHeader() {
		$this->content->form->header();
	}
	
	public function displayFormField($field) {
		$this->content->form->displayField($field);
	}
	
	public function displayFormMessages() {
		$this->content->form->displayMessages();
	}
	
	public function openForm() {
		$this->content->form->openForm();
	}
	
	public function closeForm() {
		echo '</form>';
	}
	
	public function formSuccess() {
		return $this->content->form->success;
	}
	
	public function defaultLayout() {
		$this->content->form->renderDefaultLayout();
	}
	
	public function submitButton($text, $class) {
		echo '<input type="submit" class="'.$class.'" value="'.$text.'" name="form_submit"/>';
	}
}

?>